import { describe, it, expect } from "vitest";

import { escapeHtmlTags, whereAmI } from "@/utils/misc/utils";

describe("whereAmI", () => {
  it('should return "collection" for collection URLs', () => {
    expect(whereAmI("https://www.perplexity.ai/spaces/example")).toBe(
      "collection",
    );
  });

  it('should return "thread" for thread URLs', () => {
    expect(whereAmI("https://www.perplexity.ai/search/example-query")).toBe(
      "thread",
    );
  });

  it('should return "page" for Pages URLs', () => {
    expect(whereAmI("https://www.perplexity.ai/page/example-page")).toBe(
      "page",
    );
  });

  it('should return "library" for library URLs', () => {
    expect(whereAmI("https://www.perplexity.ai/library")).toBe("library");
  });

  it('should return "home" for the home URL', () => {
    expect(whereAmI("https://www.perplexity.ai/")).toBe("home");
  });

  it('should return "settings" for the settings URL', () => {
    expect(whereAmI("https://www.perplexity.ai/account/details")).toBe(
      "settings",
    );
    expect(whereAmI("https://www.perplexity.ai/account/preferences")).toBe(
      "settings",
    );
  });

  it('should return "same_origin" for unrecognized paths on perplexity.ai', () => {
    expect(whereAmI("https://www.perplexity.ai/unknown-path")).toBe(
      "same_origin",
    );
  });

  it('should return "unknown" for non-perplexity.ai URLs', () => {
    expect(whereAmI("https://example.com")).toBe("unknown");
  });
});

describe("escapeHtmlTags", () => {
  it("should replace < with &lt;", () => {
    expect(escapeHtmlTags("<div>")).toBe("&lt;div&gt;");
  });

  it("should replace > with &gt;", () => {
    expect(escapeHtmlTags("</div>")).toBe("&lt;/div&gt;");
  });

  it("should replace both < and > in a string", () => {
    expect(escapeHtmlTags("<p>Hello, world!</p>")).toBe(
      "&lt;p&gt;Hello, world!&lt;/p&gt;",
    );
  });

  it("should not modify strings without < or >", () => {
    expect(escapeHtmlTags("Hello, world!")).toBe("Hello, world!");
  });

  it("should handle empty strings", () => {
    expect(escapeHtmlTags("")).toBe("");
  });

  it("should handle strings with multiple occurrences of < and >", () => {
    expect(escapeHtmlTags("<<div>>")).toBe("&lt;&lt;div&gt;&gt;");
  });
});
